package com.sale.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sale.common.core.utils.StringUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.system.domain.Options;
import com.sale.system.domain.PrintDictionary;
import com.sale.system.domain.SysPrintTemplate;
import com.sale.system.domain.SysTagSummary;
import com.sale.system.domain.dto.ConfigContentDto;
import com.sale.system.domain.dto.PageUrlDto;
import com.sale.system.domain.dto.SysPrintTemplateDto;
import com.sale.system.domain.dto.SysTagSummaryDto;
import com.sale.system.domain.vo.ConfigContentVo;
import com.sale.system.domain.vo.DefaultValueVo;
import com.sale.system.domain.vo.SysPrintTemplateVo;
import com.sale.system.mapper.SysMenuMapper;
import com.sale.system.mapper.SysPrintTemplateMapper;
import com.sale.system.service.ISysPrintTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;


/**
 * @Description 打印模板实现类
 * @Date 23-03-15
 * @Author hl
 **/
@Service
@RequiredArgsConstructor
public class SysPrintTemplateServiceImpl implements ISysPrintTemplateService {

    private final SysPrintTemplateMapper sysPrintTemplateMapper;

    private final SysMenuMapper sysMenuMapper;

    /**
     * 打印模板列表
     *
     * @param sysPrintTemplateVo
     * @return
     */
    @Override
    public TableDataInfo<SysPrintTemplateDto> selectPrintTemplateList(SysPrintTemplateVo sysPrintTemplateVo, PageQuery pageQuery) {
        String routing = sysPrintTemplateVo.getPrintRouting();
        if (!StringUtils.isEmpty(routing)) {
            sysPrintTemplateVo.setPrintRouting(routing.substring(1) + "/index");
        }
        Page<SysPrintTemplateDto> sysPrintTemplateDtoPage = sysPrintTemplateMapper.selectPrintTemplateList(sysPrintTemplateVo, pageQuery.build());

        return TableDataInfo.build(sysPrintTemplateDtoPage);
    }

    @Override
    public List<SysPrintTemplateDto> listNotPage(SysPrintTemplateVo sysPrintTemplateVo) {
        String routing = sysPrintTemplateVo.getPrintRouting();
        if (!StringUtils.isEmpty(routing)) {
            sysPrintTemplateVo.setPrintRouting(routing.substring(1) + "/index");
        }
        List<SysPrintTemplateDto> sysPrintTemplateDtoPage = sysPrintTemplateMapper.selectPrintTemplateListNotPage(sysPrintTemplateVo);

        return sysPrintTemplateDtoPage;
    }

    /**
     * 新增模板列表
     *
     * @param sysPrintTemplate
     * @return
     */
    @Override
    public int insertPrintTemplate(SysPrintTemplate sysPrintTemplate) {
        sysPrintTemplate.setDeptId(LoginHelper.getDeptId());
        sysPrintTemplate.setCreateBy(LoginHelper.getUsername());
        sysPrintTemplate.setPrintCode("测试");
        sysPrintTemplate.setDefaultValue(2);
        List<PageUrlDto> pageUrlList = sysMenuMapper.getPageUrlList(sysPrintTemplate.getPrintCorrelation());
        StringBuilder stringBuilder = new StringBuilder();
        for (PageUrlDto entityUrl : pageUrlList) {
            stringBuilder.append(entityUrl.getPageUrl()).append("/");
        }
        StringBuilder stringBuilder1 = stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        sysPrintTemplate.setPageUrl("/" + stringBuilder1);
        return sysPrintTemplateMapper.insertPrintTemplate(sysPrintTemplate);
    }

    /**
     * 配置内容
     *
     * @param configContentVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean configContent(ConfigContentVo configContentVo) {

        //获取模板数据
        SysPrintTemplateDto dataById = sysPrintTemplateMapper.getDataById(configContentVo.getPrintId());
        //将获取到的json对象转为字符串
        dataById.setPrintCompile(configContentVo.getStatsJson());
        SysPrintTemplate sysPrintTemplate = new SysPrintTemplate();
        BeanUtils.copyProperties(dataById,sysPrintTemplate);
        return sysPrintTemplateMapper.updateById(sysPrintTemplate) > 0;
    }

    /**
     * 配置内容查看
     *
     * @param id
     * @return
     */
    @Override
    public ConfigContentDto getView(Long id) {
        ConfigContentDto configContentDto = new ConfigContentDto();
        //获取模板信息
        SysPrintTemplateDto sysPrintTemplate = sysPrintTemplateMapper.getDataById(id);
        configContentDto.setPrintName(sysPrintTemplate.getPrintName())
            .setMenuId(sysPrintTemplate.getPrintCorrelation())
            .setPrintId(sysPrintTemplate.getPrintId())
            .setStatsJson(sysPrintTemplate.getPrintCompile())
            .setMenuName(sysMenuMapper.selectById(sysPrintTemplate.getPrintCorrelation()).getMenuName());
        return configContentDto;
    }

    /**
     * 修改默认状态
     * @param defaultValueVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDefaultValue(DefaultValueVo defaultValueVo) {
        //获取模板数据
        return sysPrintTemplateMapper.updateDefaultValue(defaultValueVo) > 0;
    }

    /**
     * 查询单个模板列表
     *
     * @param printId
     * @return SysPrintTemplateDto
     */
    @Override
    public SysPrintTemplateDto selectPrintTemplateById(long printId) {
        SysPrintTemplateVo vo = new SysPrintTemplateVo();
        vo.setPrintId(printId);
        PageQuery pageQuery = new PageQuery();
        TableDataInfo<SysPrintTemplateDto> sysPrintTemplateDtoTableDataInfo = selectPrintTemplateList(vo, pageQuery);
        List<SysPrintTemplateDto> rows = sysPrintTemplateDtoTableDataInfo.getRows();
        return rows.get(0);
    }

    /**
     * 批量删除打印模板
     *
     * @param printId
     * @return 1 成功 0 失败
     */
    @Override
    public int deletePrintTemplate(String printId) {
        String[] printIdS = printId.split(",");
        return sysPrintTemplateMapper.deletePrintTemplate(printIdS);
    }

    /**
     * 修改打印模板
     *
     * @param sysPrintTemplate
     * @return 1 成功 0 失败
     */
    @Override
    public int updatePrintTemplateEdit(SysPrintTemplate sysPrintTemplate) {
        return sysPrintTemplateMapper.updatePrintTemplateEdit(sysPrintTemplate);
    }

    /**
     * 返回JSON配置
     *
     * @param printId
     * @return String
     */
    @Override
    public String getPrintTemplateConfig(long printId) {
        return sysPrintTemplateMapper.getPrintTemplateConfig(printId);
    }

    /**
     * 查询字段
     *
     * @param printCorrelation
     * @return SysTagSummary
     */
    @Override
    public List<SysTagSummaryDto> getPrintTemplateDetails(long printCorrelation) {
        List<SysTagSummary> sysList = sysPrintTemplateMapper.getPrintTemplateDetails(printCorrelation);
        List<SysTagSummaryDto> listDto = new ArrayList<>();
        for (SysTagSummary sys :
            sysList) {
            SysTagSummaryDto dto = new SysTagSummaryDto();
            dto.setTid(sys.getTid());
            dto.setData(sys.getData());
            dto.setType(sys.getType());
            dto.setTitle(sys.getTitle());
            dto.setIcon(sys.getIcon());
            Options op = new Options();
            op.setHeight(sys.getHeight());
            op.setField(sys.getField());
            op.setFontSize(sys.getFontSize());
            op.setLineHeight(sys.getLineHeight());
            op.setTestData(sys.getTestData());
            op.setTextType(sys.getTextType());
            dto.setOptions(op);
            listDto.add(dto);
        }
        return listDto;
    }

    /**
     * @param type
     * @return
     */
    @Override
    public List<SysPrintTemplateDto> selectList(int type) {
        return sysPrintTemplateMapper.selectList(type);
    }

    /**
     * 通过模板id新增字段
     */
    @Override
    public boolean insertPrintDictionary(List<PrintDictionary> list) {
        PrintDictionary pr = list.get(0);
        int ic = sysPrintTemplateMapper.delPrintDictionary(pr.getPrintCorrelation());
        if (ic > 0) {
            sysPrintTemplateMapper.insertPrintDictionary(list);
        }
        return ic > 0;
    }
}

